package com.zztl.infiniteBand.service

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import android.support.annotation.RequiresApi
import android.support.v4.app.NotificationCompat
import com.zztl.infiniteBand.util.LogUtils
import java.io.UnsupportedEncodingException

/**
 * 通知监听服务
 *
 * @author SJL
 * @date 2017/5/22 22:21
 */
class NLService : NotificationListenerService() {

    override fun onCreate() {
        super.onCreate()
//        startForeground(1, Notification())
        startForeground()
    }


    private fun startForeground() {
        val channelId =
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    createNotificationChannel()
                } else {
                    // If earlier version channel ID is not used
                    // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
                    ""
                }

        val notificationBuilder = NotificationCompat.Builder(this, channelId )
        val notification = notificationBuilder.setOngoing(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build()
        startForeground(1, notification)
    }
    @RequiresApi(Build.VERSION_CODES.O)
    private fun createNotificationChannel(): String{
        val channelId = "my_service"
        val channelName = "My Background Service"
        val chan = NotificationChannel(channelId,
                channelName, NotificationManager.IMPORTANCE_HIGH)

        chan.importance = NotificationManager.IMPORTANCE_NONE
        chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
        val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        service.createNotificationChannel(chan)
        return channelId
    }

    override fun onNotificationPosted(sbn: StatusBarNotification) {
        LogUtils.i("swl", "接收到通知了------" + sbn.notification.tickerText + "---------" + sbn.packageName + "-------" + sbn.packageName.contains("android")
                + "---------------" + !sbn.notification.tickerText.isNullOrEmpty())
        if (sbn.packageName.equals("com.android.mms.service") && !sbn.notification.tickerText.isNullOrEmpty()) {
            try {
                /*(BillPreference.getApp(PreferenceKeys.SMSTOGGLE.name, false) as Boolean).let {
                    if (it && BraceletApplication.zhBraceletService?.bleConnectState!!) {
                        BraceletApplication.zhBraceletService?.setRemind(sbn.notification.tickerText.toString(), 4)
                    }
                }*/
            } catch (e: UnsupportedEncodingException) {
                e.printStackTrace()
            }
        }
    }


    override fun onNotificationRemoved(sbn: StatusBarNotification) {


    }


}
